package eval.deploy;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//import eval.exceptions.NotImplementedExecption;
import eval.vm.IVMFactory;
import eval.vm.VMConfigurationEnum;
import eval.vm.VirtualMachine;

/**
 * Represents an instantaneous deploy set containing all the VMs to be deployed
 * in a given instant for each type.
 * 
 * Immutable object
 * 
 * @author Nicolo' M. Calcavecchia <calcavecchia@elet.polimi.it>
 * */
public class DeploySet implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * List of VMs to be deployed in this deploy set
	 * */
	private List<VirtualMachine> deploySet = new ArrayList<VirtualMachine>();

	/**
	 * Creates an empty deploy set (nothing is deployed)
	 * */
	public DeploySet() {}
	
	public DeploySet(IVMFactory vmFactory, ArrayList<Integer> lifeTimeOfVmToDeploy) {
				for (int i = 0; i < lifeTimeOfVmToDeploy.size(); i++) {
					deploySet.add(vmFactory.createNewVirtualMachine(lifeTimeOfVmToDeploy.get(i).intValue()));
		}
		//TODO sort
		//Collections.sort(deploySet);
	}
	
	
	
	public int getNumberOfVmOfType(VMConfigurationEnum vmConfiguration) {
		int count = 0;
		for (VirtualMachine vm : deploySet) {
			if (vm.getVMConfigurationEnum().equals(vmConfiguration))
				count++;
		}
		return count;
	}

	/**
	 * Computes the size of the deploy set by summing all the VMs to be deployed
	 * of each configuration
	 * */
	public int getSize() {
		return deploySet.size();
	}

	public List<VirtualMachine> getVMs() {
		return new ArrayList<VirtualMachine>(deploySet);
	}
	
	public boolean isEmpty() {
		return (deploySet.size() == 0);
	}
}
